﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QLPM.DTO;

namespace QLPM.DAO
{
    public class DonViDAO
    {
        DataProvider provider;

        public DonViDAO()
        {
            provider = new DataProvider();
        }

        public List<DonVi> DanhSachDonVi()
        {
            String sql = "select * from DONVI";
            provider = new DataProvider("DONVI", sql);
            DataTable dt = provider;
            List<DonVi> ds = new List<DonVi>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DonVi dv = new DonVi();
                dv.MaDonVi = Convert.ToInt32(dt.Rows[i]["MaDonVi"].ToString());
                dv.TenDonVi = dt.Rows[i]["TenDonVi"].ToString();
                ds.Add(dv);
            }
            return ds;
        }

        public int ThemDV(DonVi dv)
        {
            String sql = String.Format("insert into DONVI(TenDonVi) values('{0}')", dv.TenDonVi);
            return provider.Excute_Non_Query(sql);
        }

        public int XoaDV(DonVi dv)
        {
            String sql = String.Format("delete from DONVI where MaDonVi={0}", dv.MaDonVi);
            return provider.Excute_Non_Query(sql);
        }

        public int SuaDV(DonVi dv)
        {
            String sql = String.Format("update DONVI set TenDonVi='{0}' where MaDonVi={1}", dv.TenDonVi, dv.MaDonVi);
            return provider.Excute_Non_Query(sql);
        }

        public string LayTenDonVi(int maDV)
        {
            String sql = String.Format("select * from DONVI where MaDonVi={0}", maDV);
            provider = new DataProvider("DONVI", sql);
            DataTable dt = provider;
            string TenDV = dt.Rows[0]["TenDonVi"].ToString();
            return TenDV;
        }

        public int LayMaDonVi(string tenDV)
        {
            String sql = String.Format("select * from DONVI where TenDonVi='{0}'", tenDV);
            provider = new DataProvider("DONVI", sql);
            DataTable dt = provider;
            int MaDV = Convert.ToInt32(dt.Rows[0]["MaDonVi"].ToString());
            return MaDV;
        }
    }
}
